package edu.rmit.client;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;

import edu.rmit.domain.Group;
import edu.rmit.domain.SystemInfo;
import edu.rmit.service.IGroupService;
import edu.rmit.service.ISystemInfoService;

@SuppressWarnings("serial")
public class WelcomePanel extends JPanel {
	private static int HEIGHT = 440;

	private static int WIDTH = 460;

	private JFrame frame;

	private JLabel labelWelcome;

	private JLabel labelInstructions;

	private JButton buttonLogin;

	private JButton buttonRegister;

	private JButton buttonExit;

	private JTable tableGroups;

	private Client client;

	private ISystemInfoService systemService;

	private IGroupService groupService;

	public WelcomePanel(Client client) {
		this.client = client;
		systemService = client.getSystemService();
		groupService = client.getGroupService();

		SystemInfo systemInfo = null;
		try {
			systemInfo = systemService.getSystemInfo();
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		labelWelcome = new JLabel(systemInfo.getTitle());
		labelInstructions = new JLabel(systemInfo.getContent());

		JPanel welcomePanel = new JPanel();
		welcomePanel.setSize(300, 30);

		welcomePanel.setBorder(BorderFactory.createTitledBorder("Welcome"));
		welcomePanel.add(labelWelcome);

		JPanel instructionPanel = new JPanel();
		welcomePanel.setSize(300, 70);
		instructionPanel.setBorder(BorderFactory
				.createTitledBorder("Instructions"));
		instructionPanel.add(labelInstructions);

		JPanel groupPanel = new JPanel();
		groupPanel.setBorder(BorderFactory.createTitledBorder("Groups"));
		tableGroups = getTableGroups();
		tableGroups.setSize(new Dimension(300, 300));
		tableGroups.setPreferredScrollableViewportSize(new Dimension(400, 500));

		tableGroups.getColumnModel().getColumn(0).setPreferredWidth(100);
		tableGroups.getColumnModel().getColumn(1).setPreferredWidth(200);

		groupPanel.add(tableGroups);

		JPanel panelleft = new JPanel();
		panelleft.setLayout(new GridLayout(3, 1));
		panelleft.add(welcomePanel);
		panelleft.add(instructionPanel);
		panelleft.add(groupPanel);

		JPanel panelrighttop = new JPanel();
		panelrighttop.setLayout(new GridLayout(5, 1));
		panelrighttop.add(new JLabel(""));
		buttonLogin = new JButton("Log in");
		buttonLogin.addActionListener(new ListenerLogin());
		panelrighttop.add(buttonLogin);
		panelrighttop.add(new JLabel(""));
		buttonRegister = new JButton("Register");
		buttonRegister.addActionListener(new ListenerRegister());
		panelrighttop.add(buttonRegister);
		panelrighttop.add(new JLabel(""));
		panelrighttop.setBorder(BorderFactory.createTitledBorder("Log in"));

		JPanel panelrightbottom = new JPanel();
		panelrightbottom.setLayout(new GridLayout(5, 1));
		panelrightbottom.add(new JLabel(""));
		buttonExit = new JButton("Exit");
		buttonExit.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
				System.exit(0);
			}
		});
		panelrightbottom.add(buttonExit);

		JPanel panelright = new JPanel();
		panelright.setLayout(new GridLayout(2, 1));

		panelright.add(panelrighttop);
		panelright.add(panelrightbottom);

		frame = new JFrame();
		initFrame();
		frame.add(panelleft, BorderLayout.CENTER);
		frame.add(panelright, BorderLayout.EAST);
	}

	private void initFrame() {
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);
		frame.setVisible(true);
		frame.setSize(WIDTH, HEIGHT);

		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension frameSize = frame.getSize();
		if (frameSize.height > screenSize.height) {
			frameSize.height = screenSize.height;
		}
		if (frameSize.width > screenSize.width) {
			frameSize.width = screenSize.width;
		}
		frame.setLocation((screenSize.width - frameSize.width) / 2,
				(screenSize.height - frameSize.height) / 2);
	}

	private JTable getTableGroups() {
		String[] tableTitles = new String[] { "Group Name:", "Description" };
		List<Group> groups = null;
		try {
			groups = groupService.getInterestGroups(true);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		String[][] tableDatas = new String[0][0];
		if (groups != null && !groups.isEmpty()) {
			tableDatas = new String[groups.size()][2];
		}
		for (int i = 0; i < groups.size(); i++) {
			Group group = groups.get(i);
			tableDatas[i][0] = group.getName();
			tableDatas[i][1] = group.getDescription();
		}
		JTable groupTable = new JTable(tableDatas, tableTitles) {
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};
		groupTable.setPreferredScrollableViewportSize(new Dimension(300, 200));
		return groupTable;
	}

	/**
	 * listenerone actionperformed if the sure button down.
	 * 
	 */
	class ListenerLogin implements ActionListener {

		/**
		 * Changes the text of the label.
		 * 
		 * @param event
		 *            the event object.
		 */
		public void actionPerformed(ActionEvent event) {
			frame.dispose();
			new LoginPanel(client);
		}
	}

	class ListenerRegister implements ActionListener {
		public void actionPerformed(ActionEvent event) {
			frame.dispose();
			new RegisterPanel(client);
		}
	}

	public static void main(String a[]) {
		Client client = new Client("127.0.0.1", 1099);
		client.lookUpService();
		new WelcomePanel(client);
	}

}
